<?php

namespace App\Admin\Actions\Loding;
use App\Models\yb\GoodsModel;
use App\Models\yb\NetworkGoodsModel;
use App\Models\yb\NetworkGoodsOrderDetailModel;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;


class Distribution extends RowAction
{
    public $name = '发货';

    /**
     * @param Model $model
     * @return string
     */
    public function handle(Model $model)
    {

        $order_goods_list = NetworkGoodsOrderDetailModel::where(['order_sn'=> $model->order_sn])->get();

        foreach($order_goods_list as $key=>$val){

            $network_goods_data = NetworkGoodsModel::where(['goods_id'=>$val['goods_id'],'network_id'=>$val['network_id']])->first();

            $goods_data = GoodsModel::where(['id'=>$val['goods_id']])->first();

            if(empty($network_goods_data)){
                NetworkGoodsModel::insert([
                    'network_id'=>$val['network_id'],
                    'goods_title'=>$goods_data['goods_title'],
                    'goods_type'=>$goods_data['goods_type'],
                    'goods_id'=>$goods_data['id'],
                    'goods_stock'=>$val['number'],
                    'created_at'=>date('Y-m-d H:i:s',time())
                 ]);
            }else{

                NetworkGoodsModel::where(['goods_id'=>$val['goods_id'],'network_id'=>$val['network_id']])->increment('goods_stock',$val['number']);
            }
        }

        $model->status = 4;
        $model->save();

        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * @param $star
     * @return string
     */
    public function display($id)
    {
        return '<button type="button"  class="btn btn-xs btn-flat btn-success" >发货</button>';
    }

    public function dialog()
    {
        $this->confirm('确定发货了吗？');
    }

}
